#! /usr/bin/env python
# coding=utf-8

import json
import matplotlib.pyplot as plt
from matplotlib import rcParams


# rcParams.update({
#     'font.family': 'sans-serif',
#     'font.sans-serif': ['Liberation Sans'],
# })

# plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
# plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号


def price_count(file_path):
    fre_dict = {}
    with open(file_path, "r") as f:
        while True:
            line = f.readline()
            line = line.strip("\n")
            if line:
                one_dict = json.loads(line)
                if one_dict['price'] == '暂无报价':
                    continue
                price = float(one_dict['price'])
                count = one_dict['amount'].strip("+")
                if count[-1] == "万":
                    count = float(count.strip("万")) * 10000
                else:
                    count = float(count)

                dic_ind = int(price / 100)

                fre_dict[dic_ind] = fre_dict.get(dic_ind, 0) + count

            else:
                break
    return fre_dict


def price_count_brand(file_path, brand):
    fre_dict = {}
    with open(file_path, "r") as f:
        while True:
            line = f.readline()
            line = line.strip("\n")
            if line:
                one_dict = json.loads(line)
                if one_dict['price'] == '暂无报价':
                    continue
                if not one_dict['brand'].strip("\n").strip(" ").startswith(brand):
                    continue
                price = float(one_dict['price'])
                count = one_dict['amount'].strip("+")
                if count[-1] == "万":
                    count = float(count.strip("万")) * 10000
                else:
                    count = float(count)

                dic_ind = int(price / 100)

                fre_dict[dic_ind] = fre_dict.get(dic_ind, 0) + count
            else:
                break
    return fre_dict


if __name__ == '__main__':
    ## 全网
    fig, ax = plt.subplots(figsize=(12.5, 4))
    price_count_dict = price_count("../../jd.json")
    bar_x = [k for k in sorted(price_count_dict.keys())]
    bar_h = [price_count_dict[k] for k in sorted(price_count_dict.keys())]
    plt.bar(bar_x, bar_h, width=0.2, facecolor='#9999ff', edgecolor='white')
    ax.set_xlim(-1, bar_x[-25])
    ax.set_xticks(bar_x[:-25])
    ax.set_title('price vs evaluation')
    ax.set_ylabel('evaluation volume')
    ax.set_xlabel('price range')
    plt.show()

    fig, ax = plt.subplots(figsize=(12.5, 4))
    price_count_dict = price_count_brand("../../jd.json", '美的')
    bar_x = [k for k in sorted(price_count_dict.keys())]
    bar_h = [price_count_dict[k] for k in sorted(price_count_dict.keys())]
    plt.bar(bar_x, bar_h, width=0.2, facecolor='#6666aa', edgecolor='white')
    ax.set_xlim(-1, bar_x[-10])
    ax.set_xticks(bar_x[:-10])
    ax.set_title('price vs evaluation')
    ax.set_ylabel('evaluation volume')
    ax.set_xlabel('price range')
    plt.show()

    fig, ax = plt.subplots(figsize=(12.5, 4))
    price_count_dict = price_count_brand("../../jd.json", '苏泊尔')
    bar_x = [k for k in sorted(price_count_dict.keys())]
    bar_h = [price_count_dict[k] for k in sorted(price_count_dict.keys())]
    plt.bar(bar_x, bar_h, width=0.2, facecolor='#eeee00', edgecolor='white')
    ax.set_xlim(-1, bar_x[-1])
    ax.set_xticks(bar_x[:-1])
    ax.set_title('price vs evaluation')
    ax.set_ylabel('evaluation volume')
    ax.set_xlabel('price range')
    plt.show()

    fig, ax = plt.subplots(figsize=(12.5, 4))
    price_count_dict = price_count_brand("../../jd.json", '九阳')
    bar_x = [k for k in sorted(price_count_dict.keys())]
    bar_h = [price_count_dict[k] for k in sorted(price_count_dict.keys())]
    plt.bar(bar_x, bar_h, width=0.2, facecolor='#555555', edgecolor='white')
    ax.set_xlim(-1, bar_x[-1])
    ax.set_xticks(bar_x[:-1])
    ax.set_title('price vs evaluation')
    ax.set_ylabel('evaluation volume')
    ax.set_xlabel('price range')
    plt.show()

    fig, ax = plt.subplots(figsize=(12.5, 4))
    price_count_dict = price_count_brand("../../jd.json", 'Taigroo')
    bar_x = [k for k in sorted(price_count_dict.keys())]
    bar_h = [price_count_dict[k] for k in sorted(price_count_dict.keys())]
    plt.bar(bar_x, bar_h, width=0.2, facecolor='#999999', edgecolor='white')
    ax.set_xlim(-1, 6)
    ax.set_xticks(bar_x)
    ax.set_title('price vs evaluation')
    ax.set_ylabel('evaluation volume')
    ax.set_xlabel('price range')
    plt.show()
